//统一管理咱们项目用户相关的接口
import request from '@/utils/request'
import {
  RoleInfoPageParams,
  RoleInfoRes,
  RoleMenusParams,
  RolePageRes,
  RoleParams,
} from './type'
import { AxiosResponse } from 'axios'
import qs from 'qs'
import { OptionType } from '../user/type'

// 项目用户相关的请求地址
enum Role_API {
  POST_ROLE_ADD_MENUS = '/teaching_system/role/menu',
  DELETE_ROLE_MENU = '/teaching_system/role/menu',
  GET_ROLE_PAGE = '/teaching_system/role/more',
  POST_OR_PUT_ROLE_ONE = '/teaching_system/role/one',
  GET_ROLE_INFO_BYID = '/teaching_system/role/one/{id}',
  DELETE_OR_PUT_ROLE_DELETED = '/teaching_system/role/one/deleted/{id}',
  DELETE_OR_PUT_ROLE_STATUS = '/teaching_system/role/one/status/{id}',
  GET_ROLE_OPTIONS = '/teaching_system/role/option',
}

/**
 * 为角色添加菜单(管理员)
 * @param {object} params RoleMenuForm
 * @param {number} params.roleId
 * @param {array} params.menuIds
 * @returns
 */
export const reqAddRoleMenu = (params: RoleMenusParams) =>
  request.post<any, AxiosResponse<any>>(Role_API.POST_ROLE_ADD_MENUS, params)

/**
 * 为角色删除菜单(管理员)
 * @param {object} params RoleMenuForm
 * @param {number} params.roleId
 * @param {array} params.menuIds
 * @returns
 */
export const reqDeleteRoleMenu = (params: RoleMenusParams) =>
  request.delete<any, AxiosResponse<any>>(
    Role_API.DELETE_ROLE_MENU + '?' + qs.stringify(params),
  )

/**
 * 获取角色分页
 * @param {string} pageQuery
 * @returns
 */
export const reqRoleInfoPage = (params: RoleInfoPageParams) =>
  request.get<any, AxiosResponse<RolePageRes>>(
    Role_API.GET_ROLE_PAGE + '?' + qs.stringify(params),
  )

/**
 * 添加角色
 * @param {object} params 新增角色表
 * @param {number} params.id 角色id
 * @param {string} params.name 名称
 * @param {string} params.code 编码
 * @param {number} params.sort 显示序列
 * @returns
 */
export const reqAddRole = (params: RoleParams) =>
  request.post<any, AxiosResponse<any>>(Role_API.POST_OR_PUT_ROLE_ONE, params)

/**
 * 修改角色
 * @param {object} params 新增角色表
 * @param {number} params.id 角色id
 * @param {string} params.name 名称
 * @param {string} params.code 编码
 * @param {number} params.sort 显示序列
 * @returns
 */
export const reqUpdateRole = (params: RoleParams) =>
  request.put<any, AxiosResponse<any>>(Role_API.POST_OR_PUT_ROLE_ONE, params)

/**
 * 根据id获取角色
 * @param {string} id
 * @returns
 */
export const reqRoleInfoById = (id: number) =>
  request.get<any, AxiosResponse<RoleInfoRes>>(
    Role_API.GET_ROLE_INFO_BYID.replace('{id}', id.toString()),
  )

/**
 * 恢复角色
 * @param {string} id
 * @returns
 */
export const reqRecoverRole = (id: number) =>
  request.put<any, AxiosResponse<any>>(
    Role_API.DELETE_OR_PUT_ROLE_DELETED.replace('{id}', id.toString()),
  )

/**
 * 删除角色
 * @param {string} id
 * @returns
 */
export const reqDeleteRole = (id: number) =>
  request.delete<any, AxiosResponse<any>>(
    Role_API.DELETE_OR_PUT_ROLE_DELETED.replace('{id}', id.toString()),
  )

/**
 * 取消停用角色
 * @param {string} id
 * @returns
 */
export const reqNotDisableRoleStatus = (id: number) =>
  request.put<any, AxiosResponse<any>>(
    Role_API.DELETE_OR_PUT_ROLE_STATUS.replace('{id}', id.toString()),
  )

/**
 * 停用角色
 * @param {string} id
 * @returns
 */
export const reqDisableRoleStatus = (id: number) =>
  request.delete<any, AxiosResponse<any>>(
    Role_API.DELETE_OR_PUT_ROLE_STATUS.replace('{id}', id.toString()),
  )

/**
 * 获取角色选项
 * @returns
 */
export const reqRoleOptions = () =>
  request.get<any, AxiosResponse<OptionType[]>>(Role_API.GET_ROLE_OPTIONS)
